package com.hhkj.admin.order.common.component.validator;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.course.domain.CoursePriceConf;
import com.hhkj.admin.order.common.exception.InvalidOrderItemException;
import com.hhkj.admin.order.domain.OrderItem;
import lombok.AllArgsConstructor;

import java.util.Map;

@AllArgsConstructor
public class AppCoursePriceValidateStrategy extends BaseOrderItemValidateStrategy{

    /** （科目价格id - 科目价格信息）map映射表 **/
    private Map<Long, CoursePriceConf> coursePriceConfMap;

    /** (科目id - 科目父级id)map映射表 **/
    private Map<Long, Long> courseIdParentIdMap;

    /**
     * 校验科目价格是否存在，并且该价格是否属于该科目
     * @param orderItem
     * @return
     * @throws InvalidOrderItemException
     */
    @Override
    public boolean validate(OrderItem orderItem) throws InvalidOrderItemException {
        Long parentId = courseIdParentIdMap.get(orderItem.getCourseId());
        CoursePriceConf coursePriceConf = coursePriceConfMap.get(orderItem.getCoursePriceId());
        if (ObjectUtil.isEmpty(coursePriceConf)) {
            throw new InvalidOrderItemException("该科目价格不存在");
        }
        if (coursePriceConf.getCourseId().equals(parentId) == false) {
            throw new InvalidOrderItemException("该科目价格错误");
        }
        orderItem.setPrice(coursePriceConf.getPrice());
        orderItem.setDayAmount(coursePriceConf.getDayAmount());
        return true;
    }
}
